package com.ymatou.iapi.fenqileordersync.service;

import java.util.ArrayList;
import java.util.List;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.thirdpartysync.parameter.addProductBean;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.thirdpartysync.fenqileproduct;
import com.ymttest.database.model.thirdpartysync.fenqileproductExample;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.model.thirdpartysync.fenqileskuExample;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
import com.ymttest.utils.EnvSetup;

public class FenqileOrderBaseService {

	public static addProductBean createYmtProduct(Boolean isActivity) throws Exception {
		
		// 1：创建普通洋码头商品
		// 2:检查数据是否同步到mongo
		// 3:若同步到mongo,则添加到分期乐(前端查询是从mongo库中查询，然后添加到分期乐商品表)
		addProductBean bean = new addProductBean();
		int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
		
		// 创建商品
		AddSellerProductRequest addpro = ProductManagerCallServiceV2
				.initAddSellerProductRequest(sellerId);
		addpro.getProduct().getCatalogList().remove(0);
		// addpro.getProduct().setSecondCategoryId(768);
		addpro.getProduct().setThirdCategoryId(1003);
		addpro.getProduct().setBrandId(10187);

		String productId = ProductManagerCallServiceV2.addNewProduct(addpro)
				.get("Data").getAsJsonObject().get("ProductId").getAsString();

		List<String> ids = new ArrayList<>();
		ids.add(productId);
		bean.setProductIdList(ids);
		
		if (isActivity) {
			
			// 创建活动商品
			ProductPriceCallService.getProductAllowBuyWithActivity(
					sellerId, productId, null);

			// 修改 活动商品规格 库存数量
			SellerProductCallService.updateActivityStockNum(
					sellerId, productId, 1);
		}

		return bean;
	}
	
	/**
	 * 创建分期乐商品
	 * @throws Exception
	 */
	public static addProductBean createFenqileProduct(Boolean isActivity) throws Exception {

		addProductBean addProductBean = createYmtProduct(isActivity);

		ThirdpartysyncCallService.AddProductCall(addProductBean, addProductBean
				.getProductIdList().get(0));
		
		return addProductBean;
	}
	
	public static fenqilesku getFenqileSkuId(String productId) throws InterruptedException {
		
		fenqileskuExample skuExample = new fenqileskuExample();
		thirdpartysyncWapper syncWapper = new thirdpartysyncWapper();

		fenqileproductExample productExample = new fenqileproductExample();
		productExample.createCriteria().andActionEqualTo(0)
				.andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1)
				.andYmtProductIdEqualTo(productId);
		List<fenqileproduct> prodcuts_DB = syncWapper
				.selectByExample(productExample);

		skuExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1)
				.andYmtProductIdEqualTo(productId);
		List<fenqilesku> skus_DB = syncWapper.selectByExample(skuExample);

		int times = 0;
		do {
			if (prodcuts_DB.size() == 0) {
				times += 2000;
				Thread.sleep(times);
				prodcuts_DB = syncWapper.selectByExample(productExample);
				skus_DB = syncWapper.selectByExample(skuExample);
				Logger.comment("查询数据库休眠的时间：" + times);
				if (prodcuts_DB.size() == 0)
					continue;
			} else
				break;
		} while (times <= 10000);

		if (times > 10000) {

			Logger.comment("商品同步到分期乐异常");
			return null;
		} else {
			return skus_DB.get(0);
		}
	}
	
	/**
	 * 分期乐订单转成码头订单
	 */
	public static void generateYmatouOrder() {
		
		YmatouEasyCall generateYmatouOrderCall = new YmatouEasyCall("", "POST", "JSON");

		generateYmatouOrderCall
				.setUrl("http://fenqileordersync.iapi.ymatou.com/api/sync/generateYmatouOrder");

		generateYmatouOrderCall.setData("{}");

		try {
			generateYmatouOrderCall.callService();

			Logger.comment(generateYmatouOrderCall.getReturnData());

		} catch (Exception e) {

			e.printStackTrace();
		}
	}
	
	/**
	 * 分期乐接单
	 */
	public static void onOrderAccept(long orderId) {
		
		YmatouEasyCall onOrderAcceptCall = new YmatouEasyCall("", "POST", "JSON");
		StringBuilder onOrderAcceptReq = new StringBuilder();

		
		onOrderAcceptReq.append("{\"OrderType\":4,");
		onOrderAcceptReq.append("\"OrderId\":" + orderId + "}");
		
		onOrderAcceptCall
				.setUrl("http://fenqileordersync.iapi.ymatou.com/api/onOrderAccept");

		onOrderAcceptCall.setData(onOrderAcceptReq.toString());

		try {
			onOrderAcceptCall.callService();

			Logger.comment(onOrderAcceptCall.getReturnData());

		} catch (Exception e) {

			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		FenqileOrderBaseService.createFenqileProduct(false);
	}
}
